﻿app.controller('ModuleController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.AddModuleForm = function (model) {
        model.Content = $("#noticecontent").code();
        var filelist = document.getElementById("docfiles").files;
        // 添加类目为 docfiles 的文件

        Upload.upload({
            method: 'POST',
            url: '../Module/AddModuleForm',
            data: { 'model': model},
            file: filelist
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("添加成功！");
                    setTimeout(function () {
                        location.href = "ModuleFormPageList?page=1&module=" + model.ModuleId;
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.EditModuleForm = function (model, status, page) {
        model.Content = $("#noticecontent").code();
        var filelist = document.getElementById("docfiles").files;
        // 添加类目为 docfiles 的文件

        Upload.upload({
            method: 'POST',
            url: '../Module/EditModuleForm',
            data: { 'model': model },
            file: filelist
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("更新成功！");
                    setTimeout(function () {
                        location.href = "ModuleFormPageList?page=1&module=" + model.ModuleId;
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetDepartmentList = function () {
        $scope.PageLoading = true;
        $scope.DepartmentList = [];
        $http({
            method: 'POST',
            url: '../SystemConfig/GetDepartmentList'
        }).success(function (ret) {
            $scope.PageLoading = false;
            if (ret.Code == 100) {
                $scope.DepartmentList = ret.Data;
            } else
                alertify.alert(ret.Msg);
        }).error(function (data, status, headers, config) {
            common.error($scope);
            $scope.PageLoading = false;
            alertify.alert("请检查网络");
        })
    }

    $scope.GetModuleFormPageList = function (model, page) {
        $scope.DataList = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Module/GetModuleFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.GetModuleFormDetail = function (id, v) {
        $scope.FormDetail = {};
        $http({
            method: 'POST',
            url: '../Module/GetModuleFormDetail',
            data: { "Id": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.FormDetail = ret.Data;
                    $scope.FormDetail.DepartmentId = $scope.FormDetail.DepartmentId + "";
                    $scope.FormDetail.CreateTime = $filter('JSONDate')($scope.FormDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $('#noticecontent').code($scope.FormDetail.Content);
                    if (v) {
                        $("#noticecontent").html($scope.FormDetail.Content);
                    }

                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteModuleForm = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Notice/DeleteModuleForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetModuleFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.UpdateStatusNotice = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Notice/UpdateStatusNotice',
                    data: { 'Id': id, 'status': 0 }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("操作成功！");
                            $scope.GetNoticeFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeleteModuleFile = function (Id, filelist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该文件？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Module/DeleteModuleFile',
                    data: { 'Id': Id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            for (var i = 0; i < filelist.length; i++) {
                                if (filelist[i].FileId == Id) {
                                    filelist.splice(i, 1);
                                }
                            }
                            alertify.alert("删除成功！");
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.PageLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.GetFileList = function (model) {
        $scope.FileList = model;
    }
});